<?php 
/**
 * 订单控制器
 */
class OrderController extends CommonController{
    /**
     * 
     */
      private $model;
     
	public function __auto(){
	    $this->model = K('Address');
	} 
     
     
    public function index(){
//  	p($_SESSION);
		$orderData = $_SESSION;
		$orderData = $orderData['cart'];
//		p($orderData);
		$this->assign('orderData',$orderData);
		
		$uid = $_SESSION['uid'];
//		p($uid);
		$addRess=$this->model->where("user_uid={$uid}")->all();
//		p($addRess);
		$this->assign('addRess',$addRess);
		
		
		
		
		
		
		
    	$this->display(); 
    }
	
	/**
	 * 添加地址
	 */
	public function addRess(){
		
//		p($_POST);
		if(IS_POST){
			$_POST['address'] = $_POST['province'].'&nbsp;'.$_POST['city'].'&nbsp;'.$_POST['county'].$_POST['content'];
			$_POST['user_uid'] = $_SESSION['uid'];
			if(!$this->model->addDate()){
				$this->error($this->model->error);
			}
			$this->success('添加成功');
		}
		
		
		
		
	}


		
	/**
	 * 添加订单
	 */
	public function addOrder(){
//		p($_POST);die;
//		p($_SESSION);die;
		if(IS_POST){
			if($_POST['adid']){
			//地址id
			$adid = (int)$_POST['adid'];
//			p($adid);
			//查询地址表
			$adidData = M("address")->where("adid={$adid}")->find();
//			p($adidData);
			
			$cart = new Cart();
			//生成订单号
			$orderId = $cart::getOrderId();
//			p($orderId);
			$time = time();
			
			//重组数组，添加订单表
			$data = array(
			//地址表里的
				'address' =>$adidData['address'],
				'consignee'=>$adidData['name'],
				'mobile'=>$adidData['phone'],
				'user_uid'=>$adidData['user_uid'],
				//时间
				'time'=>$time,
				//订单号
				'number'=>$orderId,
				//总价
				'total'=>$_SESSION['cart']['total'],
				//备注
				'remark'=>"无",
				//状态
				'status'=>"未付款",
				
				//把adid压入数组
				'adid'=>$_POST['adid']
				
			);
//			p($data);
			$oid = M('gorder')->add($data);
			
			//添加订单列表
			$orderList = $_SESSION['cart']['goods'];
//			p($orderList);
			//循环$orderList，获得商品信息
			foreach ($orderList as $k => $v) {
				$subtotal = $v['num']*$v['price'];
				$listData = array(
					'quantity'=>$v['num'],
					'subtotal'=>$subtotal,
					'explain'=>'',
					'goods_gid'=>$v['id'],
					'price'=>$v['price'],
					'gorder_oid'=>$oid,
					'spec'=>$v['options']['color'],
					'gname'=>$v['name']
				);
//				p($listData);die;
				M('gorder_list')->add($listData);
//				//更改货品列表的库存
//				$combine = implode(',',$v['options']);
//				p($combine);
//				//查询要更改库存的id
//				$glid = M('goods_list')->where("combine='{$combine}' AND gid={$v['id']}")->getField('glid');
			}


				//清空session里的数据(清空购物车)
				cart::delAll();
				
				
				
				$this->success('订单已生成，请尽快支付……', U('Pay/index',array('oid'=>$oid)));
			}else{
				$this->error('请选择地址');
			}
		
		
	   
		}

	}


 }
 
 
 
 
 
 
 
 
 
 
 




 ?>